package com.siblings.loan.ui.user.act

import android.os.Handler
import android.os.Looper
import android.view.View
import com.lib.base.base.BaseActivity
import com.siblings.loan.R
import com.siblings.loan.databinding.ActVersionBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.utils.dialog.KefuDialog
import com.siblings.loan.utils.event.SibEventType
import com.siblings.loan.utils.helper.SharedHelper

/**
 * SiblingsLoan
 * @Author wwdeng
 * @CreateTime: 2024/7/15 13:45
 * @Annotation：版本管理
 */
class SibVersionAct: BaseActivity() {
    lateinit var binding: ActVersionBinding
    override fun getSibRootView(): View {
        binding = ActVersionBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initSibView() {
        setSibTitleBarVisibility(View.VISIBLE)
        setSibBackBtnRes(R.mipmap.pic_back_theme)
        setSibRightBtnRes(R.mipmap.pic_kefu_theme)
        setSibTextTitle(getString(R.string.user_menu_exit))

    }

    override fun initSibListener() {
        //联系客服
        rootViewBinding.ivFun.setOnClickListener {
            showSibKefuDialog("Version Page")
            HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_CUSTOMER, "Version Page")
        }
        //退出登录
        binding.btnLoginOut.setOnClickListener {
            HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_USER_LOGINOUT,"")
            //移除已登录状态
            SharedHelper.removeSibData(SharedHelper.SIB_SPKEY.SIBLINGS_LOGIN)
            //移除用户Token
            SharedHelper.removeSibData(SharedHelper.SIB_SPKEY.SIBLINGS_TOKEN)
            //移除用户id
            SharedHelper.removeSibData(SharedHelper.SIB_SPKEY.SIBLINGS_USERID)
            Handler(Looper.getMainLooper()).postDelayed({
                onIntent(SibLoginAct::class.java)
            },300)
        }
    }

    override fun initSibData() {
        //获取版本名称
        val packageInfo = packageManager.getPackageInfo(this.packageName, 0)
        val versionCode = packageInfo.versionCode // 版本号（整数）
        binding.textVersion.text = String.format(getString(R.string.user_version),packageInfo.versionName)
    }

    /**
     * 显示客服dialog
     */
    private fun showSibKefuDialog(eventRemark: String){
        val kefuDialog = KefuDialog(this,eventRemark,object : KefuDialog.IDialogCallback{
            override fun onClick(view: View?) {
            }
        })
        kefuDialog.setCancelable(true)
        kefuDialog.setCanceledOnTouchOutside(true)
        kefuDialog.show()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_USER_VERSION_START, "")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_USER_VERSION_END, "")
    }
}